







//
//  SearchVC.m
//  searchController
//
//  Created by MB__Lee on 2018/4/13.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "SearchVC.h"

@interface SearchVC ()<UISearchResultsUpdating,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)NSMutableArray *dataMut;

@end

@implementation SearchVC
-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        
    }
    return  _tableview;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview: self.tableview];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;//必须要加，不然searchBar 会往上移动
    
    // Do any additional setup after loading the view.
}

#pragma mark --tableviewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataMut.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *dient = @"cell";
    UITableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:dient];
    if (!cell) {
        cell  = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:dient];
    }
    cell.textLabel.text = self.dataMut[indexPath.row];
    return  cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"1111");
    UIViewController *avc = [[UIViewController alloc] init];
    
    
    
    avc.view.backgroundColor  = [UIColor orangeColor];
    NSLog(@"%@",self.navigationController);
    [self.nav popViewControllerAnimated:YES];
    [self.nav pushViewController:avc animated:YES];
    
    NSLog(@"ddd");
}

-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.searchBar resignFirstResponder];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    NSPredicate *predict = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@",searchController.searchBar.text];
    
    if (self.dataMut.count != 0) {
        [self.dataMut removeAllObjects];
    }
    

    //过滤数据
    self.dataMut = [NSMutableArray arrayWithArray:[self.AMut filteredArrayUsingPredicate:predict]];
    
    
    
    
    [self.tableview reloadData];
    
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
